package com.ssm.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ssm.entity.EnterpriseEntity;
import com.ssm.entity.InvitationEntity;
import com.ssm.entity.JobEntity;
import com.ssm.entity.RecruitEntity;
import com.ssm.entity.StudentEntity;
import com.ssm.service.EnterpriseService;
import com.ssm.service.InvitationService;
import com.ssm.service.JobService;
import com.ssm.service.RecruitService;
import com.ssm.service.StudentService;

import myUtils.Response;
import myUtils.Utils;
import myUtils.myPage;

@RestController
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    @Qualifier("EnterpriseServiceImpl")
    private EnterpriseService enterpriseService;
    
    @Autowired
    @Qualifier("JobServiceImpl")
    private JobService jobService;
    
    @Autowired
    @Qualifier("InvitationServiceImpl")
    private InvitationService invitationService;
    
    @Autowired
    @Qualifier("RecruitServiceImpl")
    private RecruitService recruitService;
    
    @Autowired
    @Qualifier("StudentServiceImpl")
    private StudentService studentService;
    
	@RequestMapping(value="/info", method=RequestMethod.POST)
	public Response fillInfo(
		@ModelAttribute EnterpriseEntity companyInfo
	) {
		companyInfo.setCoId(Utils.getUUID());
		enterpriseService.save(companyInfo);
		return Response.successWrap(companyInfo);
	}
	
	/**
	 * 修改公司信息
	 * @return
	 */
	@RequestMapping(value="/info/{id}", method=RequestMethod.POST)
	public Response updateInfo(
		@PathVariable("id")String companyId,
		@ModelAttribute EnterpriseEntity companyInfo
	) {
		companyInfo.setCoId(companyId);
		System.out.println(companyInfo.getAccountId());
		enterpriseService.update(companyInfo);
		EnterpriseEntity lastestInfo = enterpriseService.findById(companyId);
		return Response.successWrap(lastestInfo);
	}
	
	/**
	 * 增加岗位信息
	 * @return
	 */
	@RequestMapping(value="/job", method=RequestMethod.POST)
	public Response createJob(
		@ModelAttribute JobEntity jobInfo
	) {
		jobInfo.setJobId(Utils.getUUID());
		jobInfo.setCreateTime(new Date());
		jobService.save(jobInfo);
		return Response.successWrap(jobInfo);
	}
	
	/**
	 * 修改岗位信息
	 * @return
	 */
	@RequestMapping(value="/job/{id}", method=RequestMethod.POST)
	public Response updateJob(
		@PathVariable("id")String jobId,
		@ModelAttribute JobEntity jobInfo
	) {
		jobInfo.setJobId(jobId);
		jobService.update(jobInfo);
		JobEntity lastestInfo = jobService.findById(jobId);
		return Response.successWrap(lastestInfo);
	}
	
	/*-----3.8号更新------*/
	
	/**
	 * 增加邀请记录
	 * @return
	 */
	@RequestMapping(value="/invitation", method=RequestMethod.POST)
	public Response createInvitation(
		@ModelAttribute InvitationEntity invitation
	) {
		invitation.setInvitationId(Utils.getUUID());
		invitation.setStatus(-1);
		invitation.setTime(new Date());
		invitationService.save(invitation);
		return Response.successWrap();
	}
	
	/**
	 * 分页获取公司的应聘记录
	 * @return
	 */
	@RequestMapping(value= "/recruit", method=RequestMethod.GET)
	public Response allRecruit(
		@RequestParam(value="companyId",required=true)String companyId,
		@RequestParam(value="page",required=false,defaultValue="1")Integer page,
		@RequestParam(value="pageNum",required=false, defaultValue= "10")Integer pageNum
	) {
		List<RecruitEntity> recruits = recruitService.findWithPageByCompany(companyId, page, pageNum);
		return Response.successWrap(recruits, myPage.pageInfo(recruits));
	}
	
	/**
	 * 分页获取该公司的邀请记录
	 * @return
	 */
	@RequestMapping(value= "/invitation", method=RequestMethod.GET)
	public Response allInvitation(
	    @RequestParam(value="companyId",required=true)String companyId,
		@RequestParam(value="page",required=false,defaultValue="1")Integer page,
		@RequestParam(value="pageNum",required=false, defaultValue= "10")Integer pageNum
	) {
		List<InvitationEntity> invitations = invitationService.findWithPageByCompany(companyId, page, pageNum);
		return Response.successWrap(invitations, myPage.pageInfo(invitations));
	}
	
	/**
	 * 分页获取该公司的邀请记录
	 * @return
	 */
	@RequestMapping(value= "/applicants", method=RequestMethod.GET)
	public Response allApplicants(
		@RequestParam(value="page",required=false,defaultValue="1")Integer page,
		@RequestParam(value="pageNum",required=false, defaultValue= "10")Integer pageNum
	) {
		List<StudentEntity> applicants = studentService.findWithPage(page, pageNum);
		return Response.successWrap(applicants, myPage.pageInfo(applicants));
	}
	
	/**
	 * 更新应聘状态
	 * @return
	 */
	@RequestMapping(value= "/updateRecruit", method=RequestMethod.POST)
	public Response updateRecruitStatus(
	    @RequestParam(value="jobId",required=true)String jobId,
	    @RequestParam(value="stuId",required=true)String stuId,
	    @RequestParam(value="status",required=true)Integer status
	) {
		RecruitEntity recruit = new RecruitEntity();
		recruit.setJobId(jobId);
		recruit.setStuId(stuId);
		recruit.setRecStatus(status);
		Boolean result = recruitService.update(recruit);
		if (result) {
			return Response.successWrap();
		} else {
			return Response.failWrap("更新应聘状态失败");
		}
		
	}
	
	/**
	 * 获取所有的岗位
	 * @param page 页数
	 * @param pageNum 每页的数量
	 * @return
	 */
	@RequestMapping(value= "/jobs", method=RequestMethod.GET)
	public Response getJobs(
		@RequestParam(value="page",required=false,defaultValue="1")Integer page,
		@RequestParam(value="pageNum",required=false, defaultValue= "10")Integer pageNum,
		@RequestParam(value="coId",required=false)String coId
	) {
		List<JobEntity> jobList = jobService.findWithPage(page, pageNum, coId);
		return Response.successWrap(jobList, myPage.pageInfo(jobList));
	}
	
	
	
	
}
